package agh.sr.lab4.bank.server;

import java.io.IOException;

import agh.sr.lab4.common.utils.BankAccountDataFileProvider;
import agh.sr.lab4.common.utils.Config;
import agh.sr.lab4.common.utils.IBankAccountDataProvider;
import Bank._BankAccountDisp;
import Ice.Current;

public class BankAccountI extends _BankAccountDisp {
	private static final long serialVersionUID = -2967364844944616004L;
	IBankAccountDataProvider accountDataProvider = new BankAccountDataFileProvider();

	public BankAccountI() {
		System.out.println("BankAccountI()");
	}

	@Override
	public synchronized void login(String pesel, Current __current) {
		System.out.println("BankAccountI.login");

	}

	@Override
	public synchronized void logoff(Current __current) {
		System.out.println("BankAccountI.logoff");

	}

	@Override
	public synchronized double getBalance(Current __current) {
		System.out.println("BankAccountI.getBalance()");

		// String pesel = __current.ctx.get(Config.PESEL);
		String pesel = __current.id.name;

		double amount = 0;
		try {
			amount = accountDataProvider.getCurrentBalance(pesel);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return amount;
	}

	@Override
	public synchronized void withdraw(double amount, Current __current) {
		System.out.println("BankAccountI.withdraw()");
		// String pesel = __current.ctx.get(Config.PESEL);
		String pesel = __current.id.name;
		try {
			double current = accountDataProvider.getCurrentBalance(pesel);
			double newBalance = current - amount;
			accountDataProvider.setNewBalance(pesel, newBalance);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public synchronized void supply(double amount, Current __current) {
		System.out.println("BankAccountI.supply()");
		// String pesel = __current.ctx.get(Config.PESEL);
		String pesel = __current.id.name;
		try {
			double current = accountDataProvider.getCurrentBalance(pesel);
			double newBalance = current + amount;
			accountDataProvider.setNewBalance(pesel, newBalance);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
